* density.do	JEP 	5/10/2015
* Builds a graph of wage densities for the formal and informal sector
* Builds on Code/Doc/densisty_graphs_binary.do

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
 else if c(username)=="A15858" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
	loc pr=0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'" 
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	loc pr=1
}
*/


* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"

* Define a program to export graphs

cap program drop mex
program define mex
	cap mkdir "`2'/Doc"
	cap mkdir "`2'/Doc/slides_lacea"
	cap mkdir "`2'/Doc/slides_lacea/fig"
	graph export "`2'/Doc/slides_lacea/fig/`1'.eps", replace
	! epstopdf "`2'/Doc/slides_lacea/fig/`1'.eps"
	erase "`2'/Doc/slides_lacea/fig/`1'.eps"
	graph export "`2'/Doc/slides_lacea/fig/`1'.pdf", replace
	graph export "`2'/Doc/slides_lacea/fig/`1'.png", replace
	* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
end	

cap program drop mex2
program define mex2
	cap mkdir "`2'/Doc"
	cap mkdir "`2'/Doc/third_year"
	cap mkdir "`2'/Doc/third_year/fig"
	graph export "`2'/Doc/third_year/fig/`1'.eps", replace
	! epstopdf "`2'/Doc/third_year/fig/`1'.eps"
	erase "`2'/Doc/third_year/fig/`1'.eps"
	graph export "`2'/Doc/third_year/fig/`1'.pdf", replace
	graph export "`2'/Doc/third_year/fig/`1'.png", replace
	* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
end	

cap program drop mex3
program define mex3
	cap mkdir "`2'/Doc"
	cap mkdir "`2'/Doc/tosubmit2"
	cap mkdir "`2'/Doc/tosubmit2/fig"
	graph export "`2'/Doc/tosubmit2/fig/`1'.eps", replace
	! epstopdf "`2'/Doc/tosubmit2/fig/`1'.eps"
	erase "`2'/Doc/tosubmit2/fig/`1'.eps"
	graph export "`2'/Doc/tosubmit2/fig/`1'.pdf", replace
	graph export "`2'/Doc/tosubmit2/fig/`1'.png", replace
	graph export "`2'/Doc/tosubmit2/ILR-Figs/Fig1_`1'.png", replace
	* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
end	

* Draw graph for formal sector
use "`master'/Data/Source/ocupados_mw.dta", clear
graph drop _all
set scheme s1color
* Choose samples
mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
mw_measures rif4sample , stub(rif4)
mytreatment, posts("tq(1999q1)") mw(bite_rif4_ind) wage(salario_mensual_real) limp(1,2,4,7)
mysample rif10sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) informal exind(13,29,42)

* mw_measures rif10sample , stub(rif10)

sum minw_mensual_real if time==tq(1999q1), meanonly
*sum minw_mensual_real_subsidio if time==tq(1999q1), meanonly
glo line=ln(r(mean)/2000)
gen count=lw<$line
sum minw_mensual_real if time==tq(1998q4), meanonly
* sum minw_mensual_real_subsidio if time==tq(1998q4), meanonly
glo oldline=ln(r(mean)/2000)
gen countold=lw<$oldline
glo bw "0.08"

su count if rif4sample & time==tq(1998q4) [w=fex], d
su countold if rif4sample & time==tq(1998q4) [w=fex],d

su count if rif10sample & time==tq(1998q4) [w=fex], d
su countold if rif10sample & time==tq(1998q4) [w=fex],d


gen lw_real_usd = log(salario_mensual_real/2000)

* Formal sector
twoway (kdensity lw_real_usd if time==tq(1998q4) & rif4sample  [aw=fex], bw(${bw}) lcolor(black) lpattern(solid) xline($oldline, lcolor(black))) (kdensity lw_real_usd if time==tq(1999q1) & rif4sample [aw=fex], bw(${bw}) lcolor(cranberry) lpattern(dash))  if inrange(lw_real_usd,3.5,5) , xline(${line}, lcolor(cranberry) lpattern(dash))   subtitle(Formal) ytitle(Density) xtitle(Log real wage) name(formal) legend(label(1 "Before minimum wage shock - 98q4") label(2 "After minimum wage shock - 99q1") rows(2)) xlabel(3.5(0.25)5, noticks) ylabel(, noticks angle(horizontal)) 

* Informal sector
twoway (kdensity lw_real_usd if time==tq(1998q4) & rif10sample [aw=fex], bw(${bw})  lcolor(black) lpattern(solid)  xline($oldline, lcolor(black))) (kdensity lw_real_usd if time==tq(1999q1) & rif10sample [aw=fex], bw(${bw}) lcolor(cranberry) lpattern(dash))  if inrange(lw_real_usd,3.5,5) , xline(${line}, lcolor(cranberry) lpattern(dash))   subtitle(Informal) ytitle(Density) xtitle(Log real wage) name(informal) xlabel(3.5(0.25)5, noticks) ylabel(, noticks angle(horizontal)) 

* Combine

grc1leg formal informal, legend(formal) xcommon ycommon rows(2)

* Export

mex density "`master'"
mex2 density "`master'"
mex3 density "`master'"

if `pr' {
	project, creates("`master'/Doc/slides_lacea/fig/density.png")
	project, creates("`master'/Doc/third_year/fig/density.pdf")
	project, creates("`master'/Doc/tosubmit2/fig/density.pdf")
}

/*

graph drop _all

* Formal sector
twoway (kdensity lw_real_usd if time==tq(1998q4) & rif4sample  [aw=fex], bw(${bw}) lcolor(black) lpattern(solid) xline($oldline, lcolor(black))) (kdensity lw_real_usd if time==tq(1999q1) & rif4sample [aw=fex], bw(${bw}) lcolor(cranberry) lpattern(dash))  if inrange(lw_real_usd,3.5,5) , xline(${line}, lcolor(cranberry) lpattern(dash))   subtitle(Formal) ytitle(Density) xtitle(Log real wage) name(formal) legend(label(1 "Before the real minimum wage increase - 98Q4") label(2 "After the real minimum wage increase - 99Q1") rows(2)) xlabel(3.5(0.25)5, noticks) ylabel(, noticks angle(horizontal)) 

* Informal sector
twoway (kdensity lw_real_usd if time==tq(1998q4) & rif10sample [aw=fex], bw(${bw})  lcolor(black) lpattern(solid)  xline($oldline, lcolor(black))) (kdensity lw_real_usd if time==tq(1999q1) & rif10sample [aw=fex], bw(${bw}) lcolor(cranberry) lpattern(dash))  if inrange(lw_real_usd,3.5,5) , xline(${line}, lcolor(cranberry) lpattern(dash))   subtitle(Informal) ytitle(Density) xtitle(Log real wage) name(informal) xlabel(3.5(0.25)5, noticks) ylabel(, noticks angle(horizontal)) 

* Combine

grc1leg formal informal, legend(formal) xcommon ycommon rows(2) title("Figure 1" "Monthly wages density" "in the formal and informal sector") ///
note("Vertical lines indicate minimum wage levels.")

loc master "B:\Col_Minimum_Wage"
graph export "`master'/Doc\Paquete DIBM\Fig/Blog_densidades.png", replace

*/
	